package com.soft.javase.day06.datetime.news;

import java.time.LocalDateTime;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/4/24
 */
public class TestNews {
    public static void main(String[] args) {
        News[] newsArray = new News[10];
        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < 10; i++) {
            newsArray[i] = new News("新闻" + (i+1),
                    "内容" + (i+1),
                    now.plusDays(i));
        }

        System.out.println("排序前:");
        for (News news : newsArray) {
            news.display();
        }

        bubbleSort(newsArray);

        System.out.println("\n排序后:");
        for (News news : newsArray) {
            news.display();
        }

    }

    public static void bubbleSort(News[] newsArray) {
        int n = newsArray.length;
        for (int i = 0; i < n-1; i++) {
            for (int j = 0; j < n-i-1; j++) {
                if (newsArray[j].getTime().isBefore(newsArray[j+1].getTime())) {
                    // 交换新闻位置
                    News temp = newsArray[j];
                    newsArray[j] = newsArray[j+1];
                    newsArray[j+1] = temp;
                }
            }
        }
    }
}
